package com.mrd.gtimp.base.entity;

import org.apache.commons.collections.CollectionUtils;
import org.hibernate.annotations.GenericGenerator;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name = "T_SYS_USER")
public class User implements Serializable {
	private static final long	serialVersionUID	= 1L;

	@Id
	@GeneratedValue(generator = "paymentableGenerator")
	@GenericGenerator(name = "paymentableGenerator", strategy = "uuid")
	@Column(name = "U_ID")
	private String				id;

	@Column(name = "U_NAME")
	private String				name;

	@Column(name = "U_PWD")
	private String				pwd;

	@Column(name = "U_MOBILE")
	private String				mobile;

	@Column(name = "U_ACCOUNT")
	private String				account;

	@Column(name = "U_TYPE")
	private String				type;

	@Column(name = "U_ISABLED")
	private String				isabled;

	@Column(name = "U_STATE")
	private String				state;

	@Column(name = "CREATOR")
	private String				creator;

	@Column(name = "CTIME")
	@Temporal(TemporalType.TIMESTAMP)
	private Date				ctime;

	@Column(name = "MODIFIER")
	private String				modifier;

	@Column(name = "MTIME")
	@Temporal(TemporalType.TIMESTAMP)
	private Date				mtime;

	@Column(name = "U_EMAIL")
	private String				email;

	@Column(name = "U_MOBILE_VALIDATE")
	private String				mvalidate;

	@Column(name = "U_TELPHONE")
	private String				telphone;

	@ManyToMany
	@JoinTable(name = "T_SYS_DEPT_USER", joinColumns = @JoinColumn(name = "U_ID"),
	        inverseJoinColumns = @JoinColumn(name = "DEPT_ID"))
	private List<Dept>			depts;

	@ManyToMany
	@JoinTable(name = "T_SYS_ROLE_USER", joinColumns = @JoinColumn(name = "USER_ID"),
	        inverseJoinColumns = @JoinColumn(name = "ROLE_ID"))
	private List<Role>			roles;

	@Transient
	private List<Permissions>	permissions;

	@Transient
	private List<Module>		modules;

	@Transient
	private List<Permissions>	managePermissions;

	@Transient
	private List<Module>		manageModules;

	@Transient
	private Boolean				checked;
	@Transient
	private String				belongCompanyName;
    @Transient
    private Company             company;

	public User() {
		super();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getMobile() {
		return mobile;
	}

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getIsabled() {
        return isabled;
    }

    public void setIsabled(String isabled) {
        this.isabled = isabled;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCreator() {
        return creator;
    }

    public void setCreator(String creator) {
        this.creator = creator;
    }

    public Date getCtime() {
        return ctime;
    }

    public void setCtime(Date ctime) {
        this.ctime = ctime;
    }

    public String getModifier() {
        return modifier;
    }

    public void setModifier(String modifier) {
        this.modifier = modifier;
    }

    public Date getMtime() {
        return mtime;
    }

    public void setMtime(Date mtime) {
        this.mtime = mtime;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Boolean getChecked() {
        return checked;
    }

    public void setChecked(Boolean checked) {
        this.checked = checked;
    }

    public String getMvalidate() {
        return mvalidate;
    }

    public void setMvalidate(String mvalidate) {
        this.mvalidate = mvalidate;
    }

    public String getTelphone() {
        return telphone;
    }

    public void setTelphone(String telphone) {
        this.telphone = telphone;
    }

    public List<Dept> getDepts() {
        return depts;
    }

    public void setDepts(List<Dept> depts) {
        this.depts = depts;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public Company getCompany() {
        if(this.company != null){
            return this.company;
        }
        if (depts == null || depts.size() == 0) {
            return null;
        }
        this.company = depts.get(0).getCompany();
        return company;
    }

    public List<Permissions> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<Permissions> permissions) {
        this.permissions = permissions;
    }

    public List<Module> getModules() {
        return modules;
    }

    public void setModules(List<Module> modules) {
        this.modules = modules;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public List<Permissions> getManagePermissions() {
        return managePermissions;
    }

    public void setManagePermissions(List<Permissions> managePermissions) {
        this.managePermissions = managePermissions;
    }

    public List<Module> getManageModules() {
        return manageModules;
    }

    public void setManageModules(List<Module> manageModules) {
        this.manageModules = manageModules;
    }

    @Override
    public String toString() {
        StringBuffer copNames = new StringBuffer();
        if (depts != null && depts.size() != 0) {
            copNames.append("[");
            for (Dept dept : depts) {
                String copName = dept.getCompany().getName();
                if (copNames.indexOf(copName) == -1) {
                    copNames.append(copName);
                    copNames.append(",");
                }
            }
            copNames.deleteCharAt(copNames.length() - 1);
            copNames.append("]");
        }
        return "User [id=" + id + ", name=" + name + ", mobile=" + mobile + ", telphone=" + telphone
                + ", account=" + account + ", 公司=" + copNames + "]";
    }

    public boolean isAdmin() {
        boolean result = false;
        for (Role role : this.getRoles()) {
            if ("admin".equals(role.getId())) {
                result = true;
                break;
            }
        }
        return result;
    }

    public String getBelongCompanyName() {
        if (CollectionUtils.isNotEmpty(depts)) {
            return depts.get(0).getCompany().getName();
        }
        return "";
    }
}
